import parentModel from "../parentModel";
export default class processModel extends parentModel {
    constructor() {
        super(processModel,util.api.ProcessModel);
        this.table='processTemplates';
        this.typeList=['process'];
        this.record = {};
        this.info = null;
        this.pointer = null;
        this.modelMap=null;
        /** modelMap格式
         * {
            table: tableName,
            featureList: new Set(),
            valueList: new Set(),
            fieldList: new Set(),
            groupList: new Set(),
            fieldMap: {
            fieldname；fieldtype
            },
          }
         */
    }
    static create(record){
        let object=new processModel();
        if(!!record){
            object.setRecord(record);
        };
        return object;
    }
    setRecord(record){
        if(!!record.type&&!!record.className){
            this.record=record;
            this._setPointer();
            if(Object.keys(this.record.web).length===0){
                this.record.web=processModel.webTpl();
                this.record.hier=processModel.hierTpl();
            }
        }else{
            this.record={};
            this._setPointer(false);
        }

    }
    async add(record){
        if(!record||!record.type||!record.className||record.type==''||record.className==''){
            return false;
        }
        let newRecord=Object.assign(entity.tpl(),record);
        let result = await util.api.ProcessModel.add(newRecord).then(data=>{
            if(!!data){
                this.setRecord(data);
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    static tpl(){
        return {
            // id: "",
            type:"",
            className:"",
            version: 1,
            published: true,
            features:[],
            values:{},
            hier:[],
            web:{},
            others:{},
            // createTime: dayjs.localeData(),
            // updateTime: (new Date()).toLocaleDateString(),
            comments: ""
        }
    }

    webToHier(){
        let hier=this.record.hier[0];
        let web=this.record.web.data;
        hier.start=web.find(item=>item.shapeType==='processStart').properties;
        hier.block=web.filter(shape=>shape.shapeType==='processBlock').map(item=>{
            return item.properties;
        });
        hier.end=web.find(item=>item.shapeType==='processEnd').properties;
        this.record.hier[0]=hier;
    }
    hierToWeb(){
        let web=this.record.web.data;
        let hier=this.record.hier[0];
        web.map(shape=>{
            if(shape.shapeType==='processStart'){
                shape.properties=hier.start;
            }
            if(shape.shapeType==='processEnd'){
                shape.properties=hier.end;
            }
            if(shape.shapeType==='processBlock'){
                let block=hier.block.find(item=>item.id===shape.id);
                shape.properties=block;
            }
        });
        this.record.web.data=web;
    }
    getHeirBlockTpl(){
        return processModel.heirBlockTpl()
    }
    /**
     * 流程配置模板
     */
    static hierTpl(){
        return [{
            start:processModel.heirStartTpl(),
            block:[
                processModel.heirBlockTpl()
            ],
            end:processModel.heirEndTpl()
        }]
    }
    static heirStartTpl(){
        return {
            id: "_start",
            //non-start/start/hangup/close，状态表明是否发起（start）、挂起为去除触发器（hangup）、关闭为流程执行完结（close）
            status: "non-start",
            name:'process',
            //指针，表明哪个节点处于活跃状态（enable=true）
            pointer:[],
            //开始节点指向哪个节点
            next: ['_o'],
            //变量表
            state:{
                var1:null,
                var2:null,
                var3:null,
                var4:null,
                var5:null,
            },
            init:[],
            saveTo:''
        }
    }
    static heirBlockTpl(){
        return {
            id: "_0",
            name: "block_0",
            enable: false,
            event:[],
            saveTo:'',
            connector:{
                '_end':{
                    listen:true,
                    listener:{
                        pointer1:{
                            type:'',
                            className:'',
                            id:'',
                            name:'',
                            fieldName:'',
                        },
                        pointer2:{
                            type:'',
                            className:'',
                            id:'',
                            name:'',
                            fieldName:'',
                        },
                        pointer3:{
                            type:'',
                            className:'',
                            id:'',
                            name:'',
                            fieldName:'',
                        }
                    },
                    condition:true,
                }
            }
        }
    }
    static heirEndTpl(){
        return {
            id:'_end',
            name:'_end',
            event:[],
            saveTo:'',
            process:[],
        }
    }
    static webTpl(){
        return {data:[
            processModel.webStartTpl(),
            processModel.webBlockTpl(),
            processModel.webEndTpl(),
            ...processModel.webLineTpl()
        ]}
    }
    static webStartTpl(){
        return {
            id: "_start",
            type: "processStart",
            shapeType: "processStart",
            title: "Start",
            properties: processModel.heirStartTpl(),
            fixed: false,
            width: 180,
            height: 80,
            x: 410,
            y: 0,
        }
    }
    static webBlockTpl(){
        return {
            id: "_0",
            type: "processBlock",
            shapeType: "processBlock",
            title: "block",
            properties: processModel.heirBlockTpl(),
            fixed: false,
            width: 180,
            height: 80,
            x: 410,
            y: 170,
        }
    }
    static webEndTpl(){
        return {
            id: "_end",
            type: "processEnd",
            shapeType: "processEnd",
            title: "End",
            properties: processModel.heirEndTpl(),
            fixed: false,
            width: 180,
            height: 80,
            x: 410,
            y: 360,
        }
    }
    static webLineTpl(){
        return [
            {
                id: "line1",
                type: "line",
                connectType: "elbow",
                cornersRadius: 0,
                customGap: 0,
                forwardArrow: "filled",
                from: "_start",
                to: "_0",
                fromSide: "bottom",
                toSide: "top",
                points: [
                    {
                        x: 500,
                        y: 80
                    },
                    {
                        x: 500,
                        y: 170
                    }
                ],
                width: 0,
                height: 90,
                stroke: "#2196F3",
                strokeWidth: 2,
                x: 500,
                y: 80
            },
            {
                id: "line2",
                type: "line",
                connectType: "elbow",
                cornersRadius: 0,
                forwardArrow: "filled",
                from: "_0",
                to: "_end",
                fromSide: "bottom",
                toSide: "top",
                points: [
                    {
                        x: 500,
                        y: 250
                    },
                    {
                        x: 500,
                        y: 360
                    }
                ],
                stroke: "#2196F3",
                strokeWidth: 2,
                height: 110,
                width: 0,
                x: 500,
                y: 250,
            }
        ]
    }
}
